package my.soft.aiteacher.teacher.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import my.soft.aiteacher.teacher.mapStruct.ConceptMapStruct;
import my.soft.aiteacher.teacher.mapper.FileUploadMapper;
import my.soft.aiteacher.teacher.pojo.ConceptDo;
import my.soft.aiteacher.teacher.mapper.ConceptMapper;
import my.soft.aiteacher.teacher.pojo.ConceptVo;
import my.soft.aiteacher.teacher.pojo.FileUpload;
import my.soft.aiteacher.teacher.service.IConceptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 基本概念，通过fileId进行关联 服务实现类
 * </p>
 *
 * @author GMCX
 * @since 2025-06-05
 */
@Service
public class ConceptServiceImpl extends ServiceImpl<ConceptMapper, ConceptDo> implements IConceptService {
    @Resource
    private ConceptMapper conceptMapper;
    @Resource
    private ConceptMapStruct conceptMapStruct;
    @Resource
    private FileUploadMapper fileUploadMapper;

    @Override
    public void upload(List<ConceptVo> conceptVo) {
        conceptMapper.delete(new QueryWrapper<ConceptDo>().in("file_id", conceptVo.stream().map(ConceptVo::getFileId).collect(Collectors.toList())));
        for (ConceptVo item : conceptVo) {
            if (Objects.isNull(item.getFileId())) {
                throw new RuntimeException("文件ID为空");
            }
            FileUpload fileUpload = fileUploadMapper.selectOne(new QueryWrapper<FileUpload>().eq("id", item.getFileId()));
            if (Objects.isNull(fileUpload)) {
                throw new RuntimeException("文件不存在,请联系管理员");
            }
            ConceptDo aDo = conceptMapStruct.toDo(item);
            conceptMapper.insert(aDo);
        }
    }

    @Override
    public List<ConceptVo> detail(Long fileId) {
        List<ConceptDo> listDo = conceptMapper.selectList(new QueryWrapper<ConceptDo>().eq("file_id", fileId));
        return listDo.stream().map(conceptMapStruct::toVo).toList();
    }
}
